import os
import cv2
import matplotlib.pyplot as plt
import numpy as np


def resize_keep_aspectratio(image_src, dst_size):
    src_h, src_w = image_src.shape[:2]
    # print(src_h, src_w)
    dst_h, dst_w = dst_size

    # 判断应该按哪个边做等比缩放
    h = dst_w * (float(src_h) / src_w)  # 按照ｗ做等比缩放
    w = dst_h * (float(src_w) / src_h)  # 按照h做等比缩放

    h = int(h)
    w = int(w)

    if h <= dst_h:
        image_dst = cv2.resize(image_src, (dst_w, int(h)))
    else:
        image_dst = cv2.resize(image_src, (int(w), dst_h))

    h_, w_ = image_dst.shape[:2]
    # print(h_, w_)

    top = int((dst_h - h_) / 2);
    down = int((dst_h - h_ + 1) / 2);
    left = int((dst_w - w_) / 2);
    right = int((dst_w - w_ + 1) / 2);

    value = [0, 0, 0]
    borderType = cv2.BORDER_CONSTANT
    # print(top, down, left, right)
    image_dst = cv2.copyMakeBorder(image_dst, top, down, left, right, borderType, None, value)

    return image_dst

def Pic2Video(file, video_name):
    fps = 10  # 视频每秒24帧
    size = (1600, 970)  # 需要转为视频的图片的尺寸
    fourcc = cv2.VideoWriter_fourcc(*'XVID')

    list = os.listdir(file)
    # list = sorted(list, key=lambda x: os.path.getmtime(os.path.join(file, x)))
    list = sorted(list)
    img12 = cv2.imread(os.path.join(file, list[0]))
    size = (img12.shape[1],img12.shape[0])
    print(size)
    videoWriter = cv2.VideoWriter(video_name, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, size)

    for i in list:
        img12 = cv2.imread(os.path.join(file, i))
        img12 = cv2.resize(img12, size)
        videoWriter.write(img12)
    videoWriter.release()
# # data1视频生成参数
# video_name1='/media/king/MyPassport0/king/data1/p0camera.mp4'
# video_name2='/media/king/MyPassport0/king/data1/rain_livox_1.mp4'
# out_video='/media/king/MyPassport0/king/data1/out_video.mp4'
# cap1 = cv2.VideoCapture(video_name1)
# cap2 = cv2.VideoCapture(video_name2)
# fps = 10  # 视频每秒10帧
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# ret2, frame2 = cap2.read()
# size = (frame2.shape[1], frame2.shape[0])
# videoWriter = cv2.VideoWriter(out_video, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, size)
# start_frame2=195

# data1视频生成参数
video_name1='/media/king/MyPassport0/king/data2/p0camera.mp4'
video_name2='/media/king/MyPassport0/king/data2/lidar.mp4'
out_video='/media/king/MyPassport0/king/data2/out_video.mp4'
cap1 = cv2.VideoCapture(video_name1)
cap2 = cv2.VideoCapture(video_name2)
fps = 10  # 视频每秒10帧
fourcc = cv2.VideoWriter_fourcc(*'XVID')
ret2, frame2 = cap2.read()
size = (frame2.shape[1], frame2.shape[0])
videoWriter = cv2.VideoWriter(out_video, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, size)
start_frame2=75
# fig = plt.figure()
# ax = fig.add_subplot(111)
np.linspace(1,100)
i=np.linspace(0,int(cap1.get(7))-1,int(cap1.get(7)))
j=i*3/2+start_frame2
i=i.astype(int)
j=j.astype(int)
for k in range(395):
    print(k,i[k],j[k])
    cap1.set(cv2.CAP_PROP_POS_FRAMES, i[k])  # 设置要获取的帧号
    cap2.set(cv2.CAP_PROP_POS_FRAMES, j[k])  # 设置要获取的帧号
    ret2, frame2 = cap2.read()
    ret1, frame1 = cap1.read()
    frame11=frame1[118:-118,:,:]
    frame1_temp = resize_keep_aspectratio(frame11, (437, 672))
    frame2[95:532, 1228:1900, :] = frame1_temp
    videoWriter.write(frame2)

    # ax.imshow(frame2)
    # plt.title(i)
    # plt.pause(0.01)

videoWriter.release()
cap1.release()
cap2.release()

pic_name='/media/king/MyPassport0/20210403/040302/040302_continuous/040302_release_超车/pic_040302_release/p0/2021-04-03_15-55-57-5.jpg'
# scenes=os.listdir(root_path)
# count=0
# for scene in scenes:
#     count+=1
#     print(scene,count)
#     image_path=root_path + '/' + scene + '/pic_040302_release'
#     if os.path.exists(image_path):
#         Pic2Video(image_path+'/p0',
#                   root_path + '/' + scene +'/'+ 'p0camera.mp4')
#         Pic2Video(image_path+'/p1',
#                   root_path + '/' + scene +'/'+ 'p1camera.mp4')